<?php

namespace app\admin\service;

use app\model\DepartmentLevelModel;
use app\model\DepartmentModel;
use app\model\UnitModel;
use app\model\UserModel;
use app\model\UserTagModel;
use PhpOffice\PhpSpreadsheet\IOFactory;
use think\facade\Log;
use think\Service;

class UserImportService extends Service
{
    public string $message;
    public int $line;
    public array $userData;//已存在的账号

    /**
     * 用户表格规则
     * @return array array[]
     */
    public function getUserConfig(): array
    {
        return [
            [
                'key' => 'A',
                'name' => '所在部门层级',
                'field' => 'department_name'
            ],
            [
                'key' => 'B',
                'name' => '登录账号',
                'field' => 'username'
            ],
            [
                'key' => 'C',
                'name' => '真实姓名',
                'field' => 'name'
            ],
            [
                'key' => 'D',
                'name' => '性别',
                'field' => 'sex'
            ],
            [
                'key' => 'E',
                'name' => '实际年龄',
                'field' => 'age'
            ],
            [
                'key' => 'F',
                'name' => '用户密码',
                'field' => 'password'
            ],
            [
                'key' => 'G',
                'name' => '培养层次',
                'field' => 'cultivation_level'
            ],
        ];
    }

    /**
     * 用户表格规则-学院制
     * @return array array[]
     */
    public function getCollegeUserConfig(): array
    {
        return [
            [
                'key' => 'A',
                'name' => '学部/年级/班级',
                'field' => 'department_name'
            ],
            [
                'key' => 'B',
                'name' => '学制阶段',
                'field' => 'stage'
            ],
            [
                'key' => 'C',
                'name' => '登录账号',
                'field' => 'username'
            ],
            [
                'key' => 'D',
                'name' => '真实姓名',
                'field' => 'name'
            ],
            [
                'key' => 'E',
                'name' => '性别',
                'field' => 'sex'
            ],
            [
                'key' => 'F',
                'name' => '实际年龄',
                'field' => 'age'
            ],
            [
                'key' => 'G',
                'name' => '用户密码',
                'field' => 'password'
            ],
            [
                'key' => 'H',
                'name' => '培养层次',
                'field' => 'cultivation_level'
            ],
        ];
    }

    /**
     * 获取性别对应值
     * @param $sex
     * @return int
     */
    public function sexValue($sex): int
    {
        $data = [
            '男' => 1,
            '女' => 2
        ];
        return $data[$sex] ?? 0;
    }

    /**
     * 咨询师标签范围
     * @return int[]
     */
    public function ConsultantLabel(): array
    {
        return [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
    }

    /**
     * 咨询师表格规则
     * @return array array[]
     */
    public function getConsultantConfig(): array
    {
        return [
            [
                'key' => 'A',
                'name' => '登录账号',
                'field' => 'username'
            ],
            [
                'key' => 'B',
                'name' => '姓名',
                'field' => 'name'
            ],
            [
                'key' => 'C',
                'name' => '性别',
                'field' => 'sex'
            ],
            [
                'key' => 'D',
                'name' => '实际年龄',
                'field' => 'age'
            ],
            [
                'key' => 'E',
                'name' => '标签',
                'field' => 'tag'
            ],
            [
                'key' => 'F',
                'name' => '用户密码',
                'field' => 'password'
            ]
        ];
    }

    /**
     * 管理员表格规则
     * @return array array[]
     */
    public function getAdminConfig(): array
    {
        return [
            [
                'key' => 'A',
                'name' => '登录账号',
                'field' => 'username'
            ],
            [
                'key' => 'B',
                'name' => '姓名',
                'field' => 'name'
            ],
            [
                'key' => 'C',
                'name' => '性别',
                'field' => 'sex'
            ],
            [
                'key' => 'D',
                'name' => '实际年龄',
                'field' => 'age'
            ],
            [
                'key' => 'E',
                'name' => '用户密码',
                'field' => 'password'
            ]
        ];
    }

    /**
     * 验证账号是否存在
     * @param $username
     * @return bool
     */
    public function isExistAccount($username): bool
    {
        if (in_array($username, $this->userData)) {
            return true;
        } else {
            $this->userData[] = $username;
            return false;
        }
    }


    /**
     * 添加普通用户-非学院制
     * @param $sheet
     * @param $data
     * @return bool
     */
    public function setUserInfo($sheet, $data): bool
    {
        try {
            $limit = $sheet->getHighestDataRow('A');
            if ($limit < 4) throw new \Exception('请正确填写信息！');
            $UnitModel = new UnitModel();
            $UserModel = new UserModel();
            $DepartmentModel = new DepartmentModel();
            $UserService = new UserService($this->app);
            $UnitInfo = $UnitModel->find($data['unit_id']);
            if (!$UnitInfo) throw new \Exception('参数错误！');
            $sheetDataNul = $limit - 3;//导入用户的数量
            if ($sheetDataNul + $UnitInfo['add_user_num'] > $UnitInfo['user_num']) throw new \Exception('超出单位限制！');
            $UnitInfo->inc('add_user_num', $sheetDataNul);
            $UnitInfo->save();
            $temData = [];
            $UserConfig = $this->getUserConfig();
            $time = time();
            //已存在部门
            $DepartmentList = $DepartmentModel->field('id,pid,name')
                ->where('unit_id', '=', $data['unit_id'])->select()->toArray();
            $DepartmentData = [];
            foreach ($DepartmentList as $department) {
                $DepartmentData[$department['pid']][$department['name']] = $department['id'];
            }
            //已存在账号
            $this->userData = $UserModel->column('username');

            //表格内容
            $sheetData = [];
            foreach ($UserConfig as $value) {
                $sheetData[$value['field']] = $sheet->rangeToArray($value['key'] . '4:' . $value['key'] . $limit, '', true, true);
            }
            $error = [];
            for ($j = 0; $j < $sheetDataNul; $j++) {
                $UserRowData = [
                    'company_id' => $data['company_id'],
                    'areas_id' => $data['areas_id'],
                    'unit_id' => $data['unit_id'],
                    'role_id' => $data['role_id'],
                    'role_mode' => $data['role_mode'],
                    'type' => $data['type'],
                    'province_id' => $data['province_id'],
                    'city_id' => $data['city_id'],
                    'area_id' => $data['area_id'],
                    'province_name' => $data['province_name'],
                    'city_name' => $data['city_name'],
                    'area_name' => $data['area_name'],
                    'time' => $time
                ];
                $i = $j + 4;
                foreach ($UserConfig as $value) {
                    $variate = trim($sheetData[$value['field']][$j][0] ?? '');
                    switch ($value['field']) {
                        case 'department_name':
                            if (empty($variate)) {
                                $error[] = '第' . $i . '行' . $value['key'] . $i . '列' . $value['name'] . '不能为空！';
                                break;
                            }
                            $department_name = explode('_', $variate);
                            $pid = 0;
                            foreach ($department_name as $k => $v) {
                                $department_id = $DepartmentData[$pid][$v] ?? 0;
                                if (!$department_id) {
                                    $pid2 = $pid;
                                    $pid = $DepartmentModel->insertGetId([
                                        'unit_id' => $data['unit_id'],
                                        'pid' => $pid,
                                        'level' => $k + 1,
                                        'name' => $v,
                                        'type' => 1,
                                        'time' => time()
                                    ]);
                                    $DepartmentData[$pid2][$v] = $pid;
                                } else {
                                    $pid = $department_id;
                                }
                            }
                            $UserRowData['department_id'] = $pid;
                            break;
                        case 'sex':
                            if (empty($variate)) {
                                $error[] = '第' . $i . '行' . $value['key'] . $i . '列' . $value['name'] . '不能为空！';
                                break;
                            }
                            $UserRowData[$value['field']] = $this->sexValue($variate);
                            if ($UserRowData[$value['field']] == 1) {
                                $UserRowData['avatar'] = $UserService->getDefaultAvatar('male');
                            } else {
                                $UserRowData['avatar'] = $UserService->getDefaultAvatar('female');
                            }
                            break;
                        case 'username':
                            if (empty($variate)) {
                                $error[] = '第' . $i . '行' . $value['key'] . $i . '列' . $value['name'] . '不能为空！';
                                break;
                            }
                            if (!validateUsername($variate)) {
                                $error[] = '第' . $i . '行' . $value['key'] . $i . '列' . '账号长度需要在8-18位之间！';
                                break;
                            }
                            if ($this->isExistAccount($variate)) {
                                $error[] = '第' . $i . '行' . $value['key'] . $i . '列' . '不可创建重复的账号,【' . $variate . '】';
                                break;
                            }
                            $UserRowData[$value['field']] = $variate;
                            break;
                        case 'password':
                            if (empty($variate)) {
                                $variate = env('USER_PASSWORD');
                            }
                            if (!validatePassword($variate)) {
                                $error[] = '第' . $i . '行' . $value['key'] . $i . '列' . '密码不满足8-18字符！';
                                break;
                            }
                            $UserRowData[$value['field']] = encrypt_password($variate);
                            break;
                        case 'age':
//                            if (empty($variate)) break;
                            $UserRowData[$value['field']] = $variate;
                            break;
                        default:
//                            if (empty($variate)) {
//                                $error[] = '第'.$i.'行'.$value['key'] . $i.'列'.$value['name'] . '不能为空！';
//                                break;
//                            }
                            $UserRowData[$value['field']] = $variate;
                            break;
                    }
                }
                if (empty($UserRowData)) break;
                $temData[] = $UserRowData;
            }
            if (!empty($error)) {
                throw new \Exception(implode("\n", $error));
            }
            if (!empty($temData)) {
                $UserModel->insertAll($temData, 200);
            }
            return true;
        } catch (\Exception $e) {
            $this->message = $e->getMessage();
            $this->line = $e->getLine();
            return false;
        }
    }

    /**
     * 添加普通用户-学院制
     * @param $sheet
     * @param $data
     * @return bool
     */
    public function setCollegeUserInfo($sheet, $data): bool
    {
        try {
            $limit = $sheet->getHighestDataRow('A');
            if ($limit < 4) throw new \Exception('请正确填写信息！');
            $UnitModel = new UnitModel();
            $UserModel = new UserModel();
            $DepartmentModel = new DepartmentModel();
            $DepartmentLevelModel = new DepartmentLevelModel();
            $UserService = new UserService($this->app);
            $UnitInfo = $UnitModel->find($data['unit_id']);
            if (!$UnitInfo) throw new \Exception('参数错误！');
            $sheetDataNul = $limit - 3;//导入用户的数量
            if ($sheetDataNul + $UnitInfo['add_user_num'] > $UnitInfo['user_num']) throw new \Exception('超出单位限制！');
            $UnitInfo->inc('add_user_num', $sheetDataNul);
            $UnitInfo->save();
            $temData = [];
            $UserConfig = $this->getCollegeUserConfig();
            $time = time();

            //已存在部门
            $DepartmentList = $DepartmentModel->field('id,pid,name')
                ->where('unit_id', '=', $data['unit_id'])->select()->toArray();
            $DepartmentData = [];
            foreach ($DepartmentList as $department) {
                $DepartmentData[$department['pid']][$department['name']] = $department['id'];
            }
            //已存在账号
            $this->userData = $UserModel->column('username');

            //表格内容
            $sheetData = [];
            foreach ($UserConfig as $value) {
                $sheetData[$value['field']] = $sheet->rangeToArray($value['key'] . '4:' . $value['key'] . $limit, '', true, true);
            }
            $error = [];
            for ($j = 0; $j < $sheetDataNul; $j++) {
                $UserRowData = [
                    'company_id' => $data['company_id'],
                    'areas_id' => $data['areas_id'],
                    'unit_id' => $data['unit_id'],
                    'role_id' => $data['role_id'],
                    'role_mode' => $data['role_mode'],
                    'type' => $data['type'],
                    'province_id' => $data['province_id'],
                    'city_id' => $data['city_id'],
                    'area_id' => $data['area_id'],
                    'province_name' => $data['province_name'],
                    'city_name' => $data['city_name'],
                    'area_name' => $data['area_name'],
                    'time' => $time
                ];
                $is_department = 1;
                $department_name = [];
                $i = $j + 4;
                foreach ($UserConfig as $value) {
                    $variate = trim($sheetData[$value['field']][$j][0] ?? '');
                    switch ($value['field']) {
                        case 'department_name':
                            if (empty($variate)) {
                                $error[] = '第' . $i . '行' . $value['key'] . $i . '列' . $value['name'] . '不能为空！';
                                break;
                            }
                            $department_name = explode('_', $variate);
                            if (count($department_name) < 3) {
                                $error[] = '第' . $i . '行' . $value['key'] . $i . '列' . '学院制新增用户必须选择第三级部门或者以下等级的部门！';
                                break;
                            }
                            $pid = 0;
                            foreach ($department_name as $v) {
                                $UserRowData['department_id'] = $pid = $DepartmentData[$pid][$v] ?? 0;
                                if (!$UserRowData['department_id']) {
                                    $is_department = 0;
                                    break;
                                }
                            }
                            break;
                        case 'stage':
                            if ($is_department) break;
                            if (empty($department_name)) {
                                $error[] = '第' . $i . '行' . $value['key'] . $i . '列' . '学部/年级/班级不能为空！';
                                break;
                            }
                            if (empty($variate)) {
                                $error[] = '第' . $i . '行' . $value['key'] . $i . '列' . $value['name'] . '不能为空！';
                                break;
                            }
                            $stage = explode('_', $variate);
                            if (count($stage) != 2) {
                                $error[] = '第' . $i . '行' . $value['key'] . $i . '列' . $value['name'] . '格式错误！';
                                break;
                            }
                            $LevelInfo = $DepartmentLevelModel->where([
                                ['name', '=', $stage[0]],
                                ['unit_id', '=', $data['unit_id']]
                            ])->find();
                            if (!$LevelInfo) {
                                $error[] = '第' . $i . '行' . $value['key'] . $i . '列' . $value['name'] . '不存在！';
                                break;
                            } else {
                                $LevelInfo = $LevelInfo->toArray();
                            }
                            $stageData = json_decode($LevelInfo['level_rules'], true);
                            $pid = 0;
                            foreach ($department_name as $k => $v) {
                                $department_id = $DepartmentData[$pid][$v] ?? 0;
                                if (!$department_id) {
                                    $pid2 = $pid;
                                    $pid = match ($k) {
                                        0 => $DepartmentModel->insertGetId([
                                            'unit_id' => $data['unit_id'],
                                            'pid' => $pid,
                                            'level' => $k + 1,
                                            'name' => $v,
                                            'type' => 2,
                                            'mode' => $LevelInfo['id'],
                                            'time' => time()
                                        ]),
                                        default => $DepartmentModel->insertGetId([
                                            'unit_id' => $data['unit_id'],
                                            'pid' => $pid,
                                            'level' => $k + 1,
                                            'name' => $v,
                                            'type' => 2,
                                            'mode' => $LevelInfo['id'],
                                            'stage' => array_search($stage[1], $stageData),
                                            'time' => time()
                                        ]),
                                    };
                                    $DepartmentData[$pid2][$v] = $pid;
                                } else {
                                    $pid = $department_id;
                                }
                            }
                            $UserRowData['department_id'] = $pid;
                            break;
                        case 'sex':
                            if (empty($variate)) {
                                $error[] = '第' . $i . '行' . $value['key'] . $i . '列' . $value['name'] . '不能为空！';
                                break;
                            }
                            $UserRowData[$value['field']] = $this->sexValue($variate);
                            if ($UserRowData[$value['field']] == 1) {
                                $UserRowData['avatar'] = $UserService->getDefaultAvatar('male');
                            } else {
                                $UserRowData['avatar'] = $UserService->getDefaultAvatar('female');
                            }
                            break;
                        case 'username':
                            if (empty($variate)) {
                                $error[] = '第' . $i . '行' . $value['key'] . $i . '列' . $value['name'] . '不能为空！';
                                break;
                            }
                            if (!validateUsername($variate)) {
                                $error[] = '第' . $i . '行' . $value['key'] . $i . '列' . '账号长度需要在8-18位之间！';
                                break;
                            }
                            if ($this->isExistAccount($variate)) {
                                $error[] = '第' . $i . '行' . $value['key'] . $i . '列' . '不可创建重复的账号,【' . $variate . '】';
                                break;
                            }
                            $UserRowData[$value['field']] = $variate;
                            break;
                        case 'password':
                            if (empty($variate)) {
                                $variate = env('USER_PASSWORD');
                            }
                            if (!validatePassword($variate)) {
                                $error[] = '第' . $i . '行' . $value['key'] . $i . '列' . '密码不满足8-18字符！';
                                break;
                            }
                            $UserRowData[$value['field']] = encrypt_password($variate);
                            break;
                        case 'age':
//                            if (empty($variate)) break;
                            $UserRowData[$value['field']] = $variate;
                            break;
                        default:
//                            if (empty($variate)) {
//                                $error[] = '第'.$i.'行'.$value['key'] . $i.'列'.$value['name'] . '不能为空！';
//                                break;
//                            }
                            $UserRowData[$value['field']] = $variate;
                            break;
                    }
                }
                if (empty($UserRowData)) break;
                $temData[] = $UserRowData;
            }
            if (!empty($error)) {
                throw new \Exception(implode("\n", $error));
            }
            if (!empty($temData)) {
                $UserModel->insertAll($temData, 200);
            }
            return true;
        } catch (\Exception $e) {
            $this->message = $e->getMessage();
            $this->line = $e->getLine();
            return false;
        }
    }

    /**
     * 添加咨询师
     * @param $sheet
     * @param $data
     * @return bool
     */
    public function setConsultantInfo($sheet, $data): bool
    {
        try {
            $limit = $sheet->getHighestDataRow('A');
            if ($limit < 4) throw new \Exception('请正确填写信息！');
            $UnitModel = new UnitModel();
            $UserModel = new UserModel();
            $UserService = new UserService($this->app);
            $UnitInfo = $UnitModel->find($data['unit_id']);
            if (!$UnitInfo) throw new \Exception('参数错误！');
            $sheetDataNul = $limit - 3;//导入用户的数量
            if ($sheetDataNul + $UnitInfo['add_consultant_num'] > $UnitInfo['consultant_num']) throw new \Exception('超出单位限制！');
            $UnitInfo->inc('add_consultant_num', $sheetDataNul);
            $UnitInfo->save();
            $temData = [];
            $tagData = [];
            $UserConfig = $this->getConsultantConfig();
            $time = time();
            //已存在账号
            $this->userData = $UserModel->column('username');

            //表格内容
            $sheetData = [];
            foreach ($UserConfig as $value) {
                $sheetData[$value['field']] = $sheet->rangeToArray($value['key'] . '4:' . $value['key'] . $limit, '', true, true);
            }
            $error = [];
            for ($j = 0; $j < $sheetDataNul; $j++) {
                $tag = [];
                $UserRowData = [
                    'company_id' => $data['company_id'],
                    'areas_id' => $data['areas_id'],
                    'unit_id' => $data['unit_id'],
                    'role_id' => $data['role_id'],
                    'role_mode' => $data['role_mode'],
                    'type' => $data['type'],
                    'province_id' => $data['province_id'],
                    'city_id' => $data['city_id'],
                    'area_id' => $data['area_id'],
                    'province_name' => $data['province_name'],
                    'city_name' => $data['city_name'],
                    'area_name' => $data['area_name'],
                    'time' => $time
                ];
                $i = $j + 4;
                foreach ($UserConfig as $value) {
                    $variate = trim($sheetData[$value['field']][$j][0] ?? '');
                    switch ($value['field']) {
                        case 'tag':
                            if (empty($variate)) {
                                $error[] = '第' . $i . '行' . $value['key'] . $i . '列' . $value['name'] . '不能为空！';
                                break;
                            }
                            $tag = explode(',', $variate);
                            foreach ($tag as $v) {
                                if (!in_array($v, $this->ConsultantLabel())) {
                                    $error[] = '第' . $i . '行' . $value['key'] . $i . '列' . '标签值【' . $v . '】不存在！';
                                    break;
                                }
                            }
                            break;
                        case 'sex':
                            if (empty($variate)) {
                                $error[] = '第' . $i . '行' . $value['key'] . $i . '列' . $value['name'] . '不能为空！';
                                break;
                            }
                            $UserRowData[$value['field']] = $this->sexValue($variate);
                            if ($UserRowData[$value['field']] == 1) {
                                $UserRowData['avatar'] = $UserService->getDefaultAvatar('male_counselor');
                            } else {
                                $UserRowData['avatar'] = $UserService->getDefaultAvatar('female_counselor');
                            }
                            break;
                        case 'username':
                            if (empty($variate)) {
                                $error[] = '第' . $i . '行' . $value['key'] . $i . '列' . $value['name'] . '不能为空！';
                                break;
                            }
                            if (!validateUsername($variate)) {
                                $error[] = '第' . $i . '行' . $value['key'] . $i . '列' . '账号长度需要在8-18位之间！';
                                break;
                            }
                            if ($this->isExistAccount($variate)) {
                                $error[] = '第' . $i . '行' . $value['key'] . $i . '列' . '不可创建重复的账号,【' . $variate . '】';
                                break;
                            }
                            $UserRowData[$value['field']] = $variate;
                            break;
                        case 'password':
                            if (empty($variate)) {
                                $variate = env('USER_PASSWORD');
                            }
                            if (!validatePassword($variate)) {
                                $error[] = '第' . $i . '行' . $value['key'] . $i . '列' . '密码不满足8-18字符！';
                                break;
                            }
                            $UserRowData[$value['field']] = encrypt_password($variate);
                            break;
                        case 'age':
//                            if (empty($variate)) break;
                            $UserRowData[$value['field']] = $variate;
                            break;
                        default:
                            if (empty($variate)) {
                                $error[] = '第' . $i . '行' . $value['key'] . $i . '列' . $value['name'] . '不能为空！';
                                break;
                            }
                            $UserRowData[$value['field']] = $variate;
                            break;
                    }
                }
                $temData[] = $UserRowData;
                if (count($tag) > 0 && !empty($UserRowData['username'])) {
                    $tagData[$UserRowData['username']] = array_unique($tag);
                }
            }
            if (!empty($error)) {
                throw new \Exception(implode("\n", $error));
            }

            if (!empty($temData) && !empty($tagData)) {
                $UserModel->startTrans();
                try {
                    $tagAddData = [];
                    $time = time();
                    foreach ($temData as $value) {
                        $uid = $UserModel->insertGetId($value);
                        if (!empty($tagData[$value['username']])) {
                            foreach ($tagData[$value['username']] as $tagId) {
                                $tagAddData[] = [
                                    'uid' => $uid,
                                    'tag_id' => $tagId,
                                    'time' => $time
                                ];
                            }
                        }
                    }
                    if (!empty($tagAddData)) {
                        $UserTagModel = new UserTagModel();
                        $UserTagModel->insertAll($tagAddData, 200);
                    }
                    $UserModel->commit();
                } catch (\Throwable $e) {
                    $UserModel->rollback();
                    Log::channel('go')->error('导入咨询师: ' . $e->getMessage());
                }
            } elseif (!empty($temData)) {
                $UserModel->insertAll($temData, 200);
            }
            return true;
        } catch (\Exception $e) {
            $this->message = $e->getMessage();
            $this->line = $e->getLine();
            return false;
        }
    }

    /**
     * 添加管理员
     * @param $sheet
     * @param $data
     * @return bool
     */
    public function setAdminInfo($sheet, $data): bool
    {
        try {
            $limit = $sheet->getHighestDataRow('A');
            if ($limit < 4) throw new \Exception('请正确填写信息！');
            $UnitModel = new UnitModel();
            $UserModel = new UserModel();
            $UserService = new UserService($this->app);
            $UnitInfo = $UnitModel->find($data['unit_id']);
            if (!$UnitInfo) throw new \Exception('参数错误！');
            $sheetDataNul = $limit - 3;//导入用户的数量
            if ($sheetDataNul + $UnitInfo['add_admin_num'] > $UnitInfo['admin_num']) throw new \Exception('超出单位限制！');
            $UnitInfo->inc('add_admin_num', $sheetDataNul);
            $UnitInfo->save();
            $temData = [];
            $UserConfig = $this->getAdminConfig();
            $time = time();
            //已存在账号
            $this->userData = $UserModel->column('username');

            //表格内容
            $sheetData = [];
            foreach ($UserConfig as $value) {
                $sheetData[$value['field']] = $sheet->rangeToArray($value['key'] . '4:' . $value['key'] . $limit, '', true, true);
            }
            $error = [];
            for ($j = 0; $j < $sheetDataNul; $j++) {
                $UserRowData = [
                    'company_id' => $data['company_id'],
                    'areas_id' => $data['areas_id'],
                    'unit_id' => $data['unit_id'],
                    'role_id' => $data['role_id'],
                    'role_mode' => $data['role_mode'],
                    'type' => $data['type'],
                    'province_id' => $data['province_id'],
                    'city_id' => $data['city_id'],
                    'area_id' => $data['area_id'],
                    'province_name' => $data['province_name'],
                    'city_name' => $data['city_name'],
                    'area_name' => $data['area_name'],
                    'time' => $time,
                    'avatar' => $UserService->getDefaultAvatar('admin')
                ];
                $i = $j + 4;
                foreach ($UserConfig as $value) {
                    $variate = trim($sheetData[$value['field']][$j][0] ?? '');
                    switch ($value['field']) {
                        case 'sex':
                            if (empty($variate)) {
                                $error[] = '第' . $i . '行' . $value['key'] . $i . '列' . $value['name'] . '不能为空！';
                                break;
                            }
                            $UserRowData[$value['field']] = $this->sexValue($variate);
                            break;
                        case 'username':
                            if (empty($variate)) {
                                $error[] = '第' . $i . '行' . $value['key'] . $i . '列' . $value['name'] . '不能为空！';
                                break;
                            }
                            if (!validateUsername($variate)) {
                                $error[] = '第' . $i . '行' . $value['key'] . $i . '列' . '账号长度需要在8-18位之间！';
                                break;
                            }
                            if ($this->isExistAccount($variate)) {
                                $error[] = '第' . $i . '行' . $value['key'] . $i . '列' . '不可创建重复的账号,【' . $variate . '】';
                                break;
                            }
                            $UserRowData[$value['field']] = $variate;
                            break;
                        case 'password':
                            if (empty($variate)) {
                                $variate = env('USER_PASSWORD');
                            }
                            if (!validatePassword($variate)) {
                                $error[] = '第' . $i . '行' . $value['key'] . $i . '列' . '密码不满足8-18字符！';
                                break;
                            }
                            $UserRowData[$value['field']] = encrypt_password($variate);
                            break;
                        case 'age':
//                            if (empty($variate)) break;
                            $UserRowData[$value['field']] = $variate;
                            break;
                        default:
                            if (empty($variate)) {
                                $error[] = '第' . $i . '行' . $value['key'] . $i . '列' . $value['name'] . '不能为空！';
                                break;
                            }
                            $UserRowData[$value['field']] = $variate;
                            break;
                    }
                }
                if (empty($UserRowData)) break;
                $temData[] = $UserRowData;
            }
            if (!empty($error)) {
                throw new \Exception(implode("\n", $error));
            }
            if (!empty($temData)) {
                $UserModel->insertAll($temData,200);
            }
            return true;
        } catch (\Exception $e) {
            $this->message = $e->getMessage();
            $this->line = $e->getLine();
            return false;
        }
    }

    /**
     * 导入用户信息
     * @param string $path
     * @param array $data
     * @return bool
     */
    public function import(string $path, array $data): bool
    {
        try {
            $ext = '.xlsx';
            $save_name = '/upload/' . date('Ym') . '/' . date('d') . '_' . time() . uniqid();
            $file_path = dirname('./public' . $save_name . $ext);
            folder_build($file_path);
            if (!file_put_contents('./public' . $save_name . $ext, file_get_contents($path))) throw new \Exception('文件加载失败！');
            $spreadSheet = IOFactory::load('./public' . $save_name . $ext);
            $sheetNames = $spreadSheet->getSheetNames();
            $sheet = $spreadSheet->getSheet(0);
            switch ($sheetNames[0]) {
                case '非学院制普通用户':
                    if ($data['role_mode'] != 3) throw new \Exception('模板错误！');
                    if (!$this->setUserInfo($sheet, $data)) throw new \Exception($this->message);
                    break;
                case '学院版制普通用户':
                    if ($data['role_mode'] != 3) throw new \Exception('模板错误！');
                    if (!$this->setCollegeUserInfo($sheet, $data)) throw new \Exception($this->message);
                    break;
                case '咨询师':
                    if ($data['role_mode'] != 2) throw new \Exception('模板错误！');
                    if (!$this->setConsultantInfo($sheet, $data)) throw new \Exception($this->message);
                    break;
                case '管理员':
                    if ($data['role_mode'] != 1) throw new \Exception('模板错误！');
                    if (!$this->setAdminInfo($sheet, $data)) throw new \Exception($this->message);
                    break;
                default:
                    throw new \Exception('模板错误！');
            }
            return true;
        } catch (\Exception $e) {
            $this->message = $e->getMessage();
            $this->line = $e->getLine();
            return false;
        }
    }

}